<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ include file="/WEB-INF/views/include.inc.jsp"%>

<script type="text/javascript">

$(document).ready(function(){
     
	$(".assignRole").click(function(){
		
		var roleId = $(this).attr("role");
		var organizationId = $("#organizationId").val();
		
		$.ajax({
	        type: "POST",
	        url: "${contextPath}/organization/assignRole",
	        data:"role.id="+roleId+"&organization.id="+organizationId,
	        success: function(json) {
	        	var obj =JSON.parse(json); 
	        	console.log(obj);
	        	if(obj.statusCode=='200'){
	        		var dialog = $.pdialog.getCurrent();
		    		$.pdialog.reload(dialog.data("url"));
	        	}
			},
			error: function() { 
			   alertMsg.error('分配角色失败！');
			}
	        		
	    });	
	});
	
	$(".undoRole").click(function(){
		
		var organizationRoleId = $(this).attr("organizationRoleId");
		
		$.ajax({
	        type: "POST",
	        url: "${contextPath}/organization/undoRole",
	        data:"id="+organizationRoleId,
	        success: function(json) {
	        	var obj =JSON.parse(json); 
	        	console.log(obj);
	        	if(obj.statusCode=='200'){
	        		var dialog = $.pdialog.getCurrent();
		    		$.pdialog.reload(dialog.data("url"));
	        	}
			},
			error: function() { 
			   alertMsg.error('分配角色失败！');
			}
	        		
	    });	
	});
});

</script>
<div class="pageContent" layoutH="0" >
	<input type="hidden" id="organizationId" value="${organization.id}"/>
	<fieldset>
		<legend>已分配角色(${organization.name})</legend>
		<table class="list" width="100%">
			<thead>
				<tr>
					<th width="80%">角色名称</th>
					<th width="20%">权限操作</th>
				</tr>
			</thead>
			<tbody id="hasRoles">
			   <c:forEach var="item" items="${organizationRoles}">
				<tr>
					<td>${item.role.name}</td>
					<td>
					
					<div class="button"><div class="buttonContent"><button organizationRoleId="${item.id}" class="undoRole">撤销</button></div></div>
					</td>
				</tr>
				</c:forEach>
			</tbody>
		</table>
	</fieldset>
	<br/>
	<fieldset>
		<legend>可分配角色(${user.realname})</legend>
		<table class="list" width="100%">
			<thead>
				<tr>
					<th width="80%">角色名称</th>
					<th width="20%">权限操作</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach var="item" items="${subRoles}"> 
				<tr>
					<td>${item.name}</td>
					<td>						
						<div class="button"><div class="buttonContent"><button role="${item.id}"  class="assignRole">分配</button></div></div>
					</td>
				</tr>	
				</c:forEach>
			</tbody>
		</table>
	</fieldset>
</div>
